var qcloud = require('../../vendor/wafer2-client-sdk/index');
var util = require('../../utils/util.js');
var config = require('../../config');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
  },
  getUserInfo: function(e) {
    wx.showLoading({
      title: '登录中',
      mask: 'true'
    });
    if (this.data.logged) return
    var that = this
    // 调用登录接口
    qcloud.login({
      success(result) {
        if (result) {
          console.log(result);
          console.log('登录成功');
          that.setData({
            userInfo: result,
            hasUserInfo: true,
            ogged: true
          })
          wx.setStorage({
            key: 'userInfo',
            data: {
              userInfo: result,
              bType: "warn",
              actionText: "退出登录"
            },
            success: function(res) {
              that.qcloudRequest()
            },
          })
        } else {
          // 如果不是首次登录，不会返回用户信息，请求用户信息接口获取
          that.qcloudRequest()
        }
      },
      fail(error) {
        console.log('登录失败', error)
      }
    })

  },
  onLoad: function(options) {
    var scene = decodeURIComponent(options.scene);
    var that = this;
    if (scene == 'undefined') {
      app.globalData._pid = '';
    } else {
      app.globalData._pid = scene.slice(5);
    }
    // 查看是否授权
    wx.getSetting({
      success: function(res) {
        if (res.authSetting['scope.userInfo']) { //用户已经授权过
          that.setData({
            loading: true
          })
          wx.getUserInfo({
            success: function(res) {
              wx.showLoading({
                title: '登录中',
                mask: 'true'
              });
              wx.getStorage({
                key: 'userInfo',
                success: function(result) {
                  // console.log(result.data);
                  if (result.data) {
                    // 存在用户登录信息

                    console.log('存在用户登录信息');
                    that.qcloudRequest()
                  } else {
                    console.log('不存在用户登录信息');
                  }
                },
                fail(error) {
                  console.log('不存在用户登录信息');
                  qcloud.login({

                    success(result) {
                      if (result) {
                        console.log(result);
                        console.log('登录成功');
                        that.setData({
                          userInfo: result,
                          hasUserInfo: true,
                          ogged: true
                        })
                        wx.setStorage({
                          key: 'userInfo',
                          data: {
                            userInfo: result,
                            bType: "warn",
                            actionText: "退出登录"
                          },
                          success: function(res) {
                            console.log("存储成功1")
                            that.qcloudRequest()

                          },
                        })
                      } else {
                        // 如果不是首次登录，不会返回用户信息，请求用户信息接口获取
                        that.qcloudRequest()
                      }
                    },
                    fail(error) {
                      console.log('登录失败', error)
                    }
                  })
                }
              })
            }
          });
        }
      }
    })

  },
  qcloudRequest: function() {
    var that = this;
    qcloud.request({
      url: config.service.requestUrl,
      login: true,
      success(result) {
        console.log(result);
        console.log('登录成功');
        if (result.data.code == '4004') {
          wx.clearStorageSync();
          that.onLoad({});
        }
        wx.setStorageSync("session_key", result.data.data.sessionKey);
        that.setData({
          userInfo: result.data.data,
          hasUserInfo: true,
          ogged: true,
        })
        app.globalData.userInfo = result.data.data;
        app.init();
        // console.log(app.globalData.userInfo);
        // 将数据存储在本地缓存中指定的 key 中，会覆盖掉原来该 key 对应的内容，这是一个异步接口。
        wx.setStorage({
          key: 'userInfo',
          data: {
            userInfo: result.data.data,
            bType: "warn",
            actionText: "退出登录"
          },
          success: function(res) {
            console.log(res)
            // 跳转
            if (result.data.data.tel)
              wx.switchTab({
                url: '../index/index'
              })
            else
              wx.reLaunch({
                url: '../login/login'
              })
          },
        })
      },
      fail(error) {
        console.log('request fail', error)
      }
    })

  }
})